#!/bin/bash
# @Author: leiwang
# @Date:   2018-10-08 12:32:56
# @Last Modified by:   leiwang
# @Last Modified time: 2018-11-02 15:31:36
# 1，将monitor启动命令，添加到autorunfile中，支持开机启动
# 2，创建日志文件存放目录./log
date
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/sbin:/bin:${PATH}
# 可执行文件名配置
agent="dbAgent"
monitor="dbMonitor"
# 安装特征码
featurecode='DBaudit_dbagent_V2018101600'

forceFlag=$1

# 卸载异常检查项
checkUser(){
  for i in `groups `; do
    if [ $i == "root" ]; then
      return 1
    fi
  done
  return 0
}
checkUser
ret=`echo $?`
if [ $ret != 1 ];then
    echo "WARN: User is not root!"
    #exit 1
fi


# 动态获取脚本文件所在绝对路径
RUN_PATH=`cd $(dirname $0); pwd -P`


# 找到支持开机重启的文件，用${autorunfile}表示
# centos /etc/rc.d/rc.local，若/etc/rc.local不是对前者的软链接则不会执行
# ubuntu, debian /etc/init.d/rc.local，若存在/etc/rc.local也会被执行
# suse /etc/rc.d/after.local
# 
if [[ `cat /proc/version | grep coreos | wc -l` -gt 0 ]]; then
    # 假自启文件
    autorunfile="/etc/rc.local"
    if [ ! -e /etc/rc.local ]; then
        touch /etc/rc.local
        echo '#!/bin/bash -e' >> /etc/rc.local
        echo '# This file made by dbagent' >> /etc/rc.local
        chmod +x /etc/rc.local
    fi
elif [ ` lsb_release -a 2>/dev/null | grep -i "Ubuntu" | wc -l` -gt 0 ]; then
    autorunfile="/etc/rc.local"
    if [ ! -e /etc/rc.local ]; then
        touch /etc/rc.local
        echo '#!/bin/bash -e' >> /etc/rc.local
        echo '# This file made by dbagent' >> /etc/rc.local
        chmod +x /etc/rc.local
    fi
elif [ -e /etc/rc.d/rc.local ]; then
    # centos
    autorunfile="/etc/rc.d/rc.local"
elif [ -e /etc/rc.local ]; then
    # ubuntu, debian
    autorunfile="/etc/rc.local"
elif [ "`uname -a | grep -i debian | wc -l | tr -d ' '`"  -gt "0" ]; then
    # 9.X的Debian
    touch /etc/rc.local
    echo '#!/bin/sh -e' >> /etc/rc.local
    echo '# This file made by dbagent' >> /etc/rc.local
    chmod +x /etc/rc.local
    systemctl start rc-local
    autorunfile="/etc/rc.local"
else
    # suse, 可能有部分ubuntu也存在这个分支里目前 
    if [ ! -e /etc/rc.d/after.local ]; then
        touch /etc/rc.d/after.local
        echo '#!/bin/sh -e' >> /etc/rc.d/after.local
        echo '# This file made by dbagent' >> /etc/rc.d/after.local
    fi
    autorunfile="/etc/rc.d/after.local"
fi

# 删除启动文件中的开机重启命令，特征码
if [[ `cat /proc/version | grep coreos | wc -l` -gt 0 ]]; then
    systemctl disable dbagent
fi
temNAME=`echo ${RUN_PATH}/${monitor} | sed 's#\/#\\\/#g'`
if [ "`grep ${RUN_PATH}/${monitor} ${autorunfile} | wc -l | tr -d ' '`"  -gt "0" ]; then
    # 当前目录有安装
	if [[ $forceFlag == "force" ]];then
		#升级回退需保留特征码
		echo "upgrade restore mode skip delete feature code".
	else
		eval sed -i '/${featurecode}/d' ${autorunfile}
	fi
    eval sed -i '/${temNAME}/d' ${autorunfile}
fi

# 检查卸载结果
if [ "`grep ${RUN_PATH}/${monitor} ${autorunfile} | wc -l | tr -d ' '`"  -eq "0" ]; then
    echo "Uninstall dbagent success"
else
    echo "Uninstall dbagent failed"
fi

if [[ $forceFlag == "force" ]];then
	bash -vx ${RUN_PATH}/dbagent_stop.sh
else
	bash ${RUN_PATH}/dbagent_stop.sh
fi

#rm -rf  ${RUN_PATH}/log 1>/dev/null 2>/dev/null
